package com.webserver.core;

import com.webserver.http.HttpResponse;
import com.webserver.http.HttpResquest;
import java.io.*;
import java.net.Socket;

public class ClientHandler implements Runnable{
    private Socket socket;
    public ClientHandler(Socket socket) {
        this.socket=socket;
    }
    public void run(){
        try {
            //解析请求
            HttpResquest httpResquest =new HttpResquest(socket);
            HttpResponse httpResponse = new HttpResponse(socket);

            //处理请求
            DispatcherServlet dispatcherServlet = new DispatcherServlet();
            dispatcherServlet.service(httpResquest,httpResponse);
            //发送响应
            httpResponse.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
